/**
 * Created on 19/04/2008
 * 
 */
package com.apbetioli.mapr.project.entity;

import com.apbetioli.mapr.project.schema.CtProject;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Persistent entity class for projects.
 * 
 * @author Alexandre Parra Betioli
 */
@Entity
@Table(name = "PROJECT")
@NamedQueries({
    @NamedQuery(name = "Project.findByName",  query = "SELECT p FROM Project p WHERE p.name = :name"),
    @NamedQuery(name = "Project.findByEmail", query = "SELECT p FROM Project p WHERE p.email = :email"),
    @NamedQuery(name = "Project.listAll",     query = "SELECT p FROM Project p ORDER BY p.name ASC")
})
public class Project extends CtProject implements java.io.Serializable {
    
    /*---------------------------------------------*/
    // Columns
    /*---------------------------------------------*/
    
    @Id
    @Column(name = "PKEY")
    @Override
    public String getKey() {
        return super.getKey();
    }
    
    @Column(name = "NAME")
    @Override
    public String getName() {
        return super.getName();
    }
    
    @Column(name = "EMAIL")
    @Override
    public String getEmail() {
        return super.getEmail();
    }
    
    @Embedded
    public FlickrData getFlickrData() {
        if (super.getFlickrData() == null)
            return null;

        FlickrData g = new FlickrData();
        g.parse(super.getFlickrData());
        return g;
    }

    /*---------------------------------------------*/
    // Setters
    /*---------------------------------------------*/
    
    public void setFlickrData(FlickrData value) {
        super.setFlickrData(value);
    }
    
    /*---------------------------------------------*/
    // Methods
    /*---------------------------------------------*/
        
    /**
     * It only generates a key if not informed
     */
    public void parse( final CtProject ctProject ) {
        this.setKey(ctProject.getKey() );
        this.setName(ctProject.getName() );
        this.setEmail( ctProject.getEmail() );
        this.setFlickrData( ctProject.getFlickrData() );
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getName()       != null ? getName().hashCode()       : 0);
        hash += (getKey()        != null ? getKey().hashCode()        : 0);
        hash += (getEmail()      != null ? getEmail().hashCode()      : 0);
        hash += (getFlickrData() != null ? getFlickrData().hashCode() : 0 );
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Project)) {
            return false;
        }
        Project other = (Project) object;
        if (((this.getName() == null && other.getName() != null) || 
             (this.getName() != null && !this.getName().equals(other.getName()))) 
           &&
            ((this.getKey() == null && other.getKey() != null) || 
             (this.getKey() != null && !this.getKey().equals(other.getKey()))) 
           &&
            ((this.getEmail() == null && other.getEmail() != null) || 
             (this.getEmail() != null && !this.getEmail().equals(other.getEmail())))              
           &&
            ((this.getFlickrData() == null && other.getFlickrData() != null) || 
             (this.getFlickrData() != null && !this.getFlickrData().equals(other.getFlickrData())))              

           ) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String res = "com.googlepages.apbetioli.projects.entity.Project[name=" + getName() + ", key=" + getKey() + ", email=" + getEmail();
        if( getFlickrData() != null )
            res += ", " + getFlickrData();
        res += "]";
        
        return res;
    }
    
    @Override
    public Project clone() {
        Project p = new Project();
        p.setName( this.getName() );
        p.setEmail( this.getEmail() );
        p.setKey( this.getKey() );
        
        if( this.getFlickrData() != null )
            p.setFlickrData( this.getFlickrData().clone() );
        else 
            p.setFlickrData( null );
        
        return p;
    }
    
}
